/**
 * OSAmI: Open Source Ambient Intelligence
 * Copyright (C) 2010 Bull S.A.S.
 * Contact: osami-general@osami-commons.org
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 */

package org.oasmi.gateway.storage.naive.internal;

import org.osami.gateway.sensor.data.SenseData;
import org.osami.gateway.storage.naive.internal.NaiveSenseDataStorage;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 * Created by IntelliJ IDEA.
 * User: guillaume
 * Date: 27 mai 2010
 * Time: 16:05:43
 * To change this template use File | Settings | File Templates.
 */
public class NaiveSenseDataStorageTestCase {

    private NaiveSenseDataStorage storage;

    @BeforeMethod
    public void setUp() {
        // Allow a window event of 30 seconds
        storage = new NaiveSenseDataStorage(30, 4);
    }

    @Test
    public void testSensorDataReductionWithTimeFiltering() {
        SenseData data1 = new MockSenseData("one", 1000l);
        SenseData data3 = new MockSenseData("two", System.currentTimeMillis());

        storage.store(data1);
        // As this event is older than our limit, it will be removed
        Assert.assertFalse(storage.iterator().hasNext());

        storage.store(data3);
        // As this event is younger than our limit, it will be stored
        Assert.assertTrue(storage.iterator().hasNext());

    }

    @Test
    public void testSensorDataReductionWithNumberFiltering() {
        SenseData data1 = new MockSenseData("one", System.currentTimeMillis());
        SenseData data2 = new MockSenseData("two", System.currentTimeMillis());
        SenseData data3 = new MockSenseData("three", System.currentTimeMillis());
        SenseData data4 = new MockSenseData("four", System.currentTimeMillis());
        SenseData data5 = new MockSenseData("five", System.currentTimeMillis());
        SenseData data6 = new MockSenseData("six", System.currentTimeMillis());

        storage.store(data1);
        storage.store(data2);
        storage.store(data3);
        storage.store(data4);
        storage.store(data5);
        storage.store(data6);

        int count = 0;
        for (SenseData senseData : storage) {
            count++;
        }

        Assert.assertEquals(count, 4);

    }
}
